#!/usr/bin/env python
#
# Copyright 2015 Grand Share team (NSC2015).
#
import webapp2
import json
import logging
from accounts import *


class AccessHandler(webapp2.RequestHandler):
    def get(self):
        box_id = self.request.get('box_id')
        if box_id != '':
            resp = {'status':'OK'}
            user_ids = []
            access_tokens = []
            box = RPiBox.query(RPiBox.id==box_id).fetch(1)
            if len(box) > 0:
                for user_id in box[0].users:
                    user = User.query(User.id==user_id).fetch(1)
                    if len(user) > 0:
                        user_ids.append(user[0].id)
                        access_tokens.append(user[0].access_token)
            resp['users'] = user_ids
            resp['access_tokens'] = access_tokens
        else:
            resp = {'status':'ERROR'}
        self.response.write(json.dumps(resp))


app = webapp2.WSGIApplication([
    ('/access', AccessHandler),
], debug=True)

